/* @(#)ProviderType.java    V1.0
 *
 * Project: APEP 
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *       {CREATE_TIME} First release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2007 Auto-Parts E-Procurement Co., Ltd. 
 *        No.251-1 Wuning Road, Shanghai, 200063, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Auto-Parts E-Procurement Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *   
 * 
 */
package system.base;

import system.data.ProviderTypePageData;
import framework.core.*;

/*
	Author    :
	CreateTime:{CREATE_TIME}
*/
public class ProviderTypeSrv extends IHandler
{

	public ProviderTypeSrv() 
	{
		super();
	}
	private void deleteProviderType(DBConnection con,SrvContext context) throws Exception
	{
		int[] ids = context.getInts("ids");
		ProviderType obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = ProviderType.fetch(con,String.valueOf(ids[i]));
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveProviderType(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		int id = context.getInt("Id",Value.NULL_INT);
		ProviderType obj;
		if(id==Value.NULL_INT)   //add
		{
			obj = ProviderType.fetch();
    	}
    	else                     //update
    	{
    		obj = ProviderType.fetch(con,String.valueOf(id));
    	}
		ProviderTypePageData data = (ProviderTypePageData)PageData.fromForm(context,ProviderTypePageData.class);
		context.setValue("ProviderTypePageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listProviderType(DBConnection con,SrvContext context,String path) throws Exception
	{
		ProviderTypeSearchEvent event = (ProviderTypeSearchEvent)SearchEvent.fromForm(context,ProviderTypeSearchEvent.class);
		context.setSessionValue("ProviderTypeSearchEvent",event);
		context.setNextUrl(path+"ProviderTypeList.jsp");
	}
	private void backProviderType(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"ProviderTypeList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "system/base";
		
		if(action_code.equalsIgnoreCase("listProviderType"))
		{
			listProviderType(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewProviderType"))
		{
			int id = context.getInt("Id");
			ProviderType obj = ProviderType.fetch(con,String.valueOf(id));
			context.setValue("ProviderType",obj);
			context.setNextUrl(path+"ProviderTypeView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteProviderType"))
		{
			context.setErrorUrl(path+"ProviderTypeList.jsp");
			deleteProviderType(con,context);
			backProviderType(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddProviderType"))
		{
			ProviderType obj = ProviderType.fetch();
			ProviderTypePageData data = (ProviderTypePageData)PageData.fromTable(obj,ProviderTypePageData.class);
			context.setValue("ProviderTypePageData",data);
			context.setNextUrl(path+"ProviderTypeEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateProviderType"))
		{
			String id = context.getString("Id");
			ProviderType obj = ProviderType.fetch(con,id);
			ProviderTypePageData data = (ProviderTypePageData)PageData.fromTable(obj,ProviderTypePageData.class);
			context.setValue("ProviderTypePageData",data);
			context.setNextUrl(path+"ProviderTypeEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyProviderType"))
		{
			String id = context.getString("Id");
			ProviderType obj = ProviderType.copy(ProviderType.fetch(con,id));
			ProviderTypePageData data = (ProviderTypePageData)PageData.fromTable(obj,ProviderTypePageData.class);
			context.setValue("ProviderTypePageData",data);
			context.setNextUrl(path+"ProviderTypeEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveProviderType"))
		{
			context.setErrorUrl(path+"ProviderTypeEdit.jsp");
			saveProviderType(con,context,loginContext);
			backProviderType(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewProviderType"))
		{
			context.setErrorUrl(path+"ProviderTypeEdit.jsp");
			saveProviderType(con,context,loginContext);
			ProviderType obj = ProviderType.fetch();
			ProviderTypePageData data = (ProviderTypePageData)PageData.fromTable(obj,ProviderTypePageData.class);
			context.setValue("ProviderTypePageData",data);
			context.setNextUrl(path+"ProviderTypeEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListProviderType"))
		{
			backProviderType(con,context,path);
			return;
		}
		
		//add by  start

		//add by  end

		Logger.throwException("ProviderTypeSrv","unknown action_code="+action_code);
	}
}
